//
//  JWMainViewController.m
//  见闻新闻
//
//  Created by brother on 16/2/27.
//  Copyright © 2016年 JoanBoat. All rights reserved.
//

#import "JWMainViewController.h"

#import "JWCategoryCollectionViewCell.h"

#import "JWCategoryModel.h"

#import "JWCateoryCollectionViewLayout.h"

#import "AppDelegate.h"

#import "JWNewsController.h"

#define  With  [UIScreen mainScreen].bounds.size.width

@interface  JWMainViewController()
<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout,UINavigationBarDelegate>

@property (weak, nonatomic) IBOutlet UICollectionView *collectionView;



@property (nonatomic ,strong)NSMutableArray *dataArray;


@end

@implementation JWMainViewController

-(NSMutableArray *)dataArray {
    if (_dataArray == nil) {

        _dataArray = [JWCategoryModel categoryModelArray];
    }
    return _dataArray;

}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {

    [super touchesBegan:touches withEvent:event];
}

-(void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationController.navigationBar.delegate = self;
    
    UICollectionViewFlowLayout *layout = [[JWCateoryCollectionViewLayout alloc]init];
  
    
    layout.minimumInteritemSpacing = 0;
    layout.minimumLineSpacing = 0;

    self.collectionView.collectionViewLayout = layout;
    
    
    self.collectionView.frame =CGRectMake(0, 64, With,[UIScreen mainScreen].bounds.size.height);   
}


- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBar.userInteractionEnabled = NO;
    AppDelegate *app = (AppDelegate *)([UIApplication sharedApplication].delegate);
    app.isEnableUserInterface = YES;

}


- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.dataArray.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    JWCategoryCollectionViewCell *cell = [JWCategoryCollectionViewCell collectionCellWithCollectionView:collectionView withIndexPath:indexPath];

    cell.JWMyCategoryModel = self.dataArray[indexPath.row];
    cell.backgroundColor = [UIColor whiteColor];
    cell.layer.borderWidth = 0.5;
    
    return cell;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    return CGSizeMake(With,200);
}


-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    
    UIStoryboard *story = [UIStoryboard storyboardWithName:@"Home" bundle:nil];
    
    JWNewsController *newsControl = [story instantiateViewControllerWithIdentifier:@"newsControl"];
    
     __weak typeof (self)tempSelf = self;
    
    newsControl.categoryBLock = ^JWCategoryModel *{
        return tempSelf.dataArray[indexPath.row];
    };
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    app.isEnableUserInterface = NO;
    
    [self.navigationController pushViewController:newsControl animated:YES];
    
    
}

@end
